package com.gurun.camera.addDevice.model;

import com.danale.sdk.Danale;
import com.danale.sdk.platform.entity.deviceinfo.DeviceAddedOnlineInfoV4;
import com.danale.sdk.platform.entity.deviceinfo.DeviceBaseInfo;
import com.danale.sdk.platform.result.deviceinfo.v4.DeviceAddedOnlineResultV4;
import com.danale.sdk.platform.result.deviceinfo.v4.DeviceBaseInfoResultV4;
import com.gurun.camera.addDevice.entity.DeviceInfoEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/14.
 */
public class QueryDevModelImpl implements IQueryDevInfoModel {

    private static final String TAG = QueryDevModelImpl.class.getSimpleName();

    @Override
    public Observable<List<DeviceInfoEntity>> obtainDeviceInfos(final List<DeviceInfoEntity> list) {
        return Observable.just(list)
                    .subscribeOn(Schedulers.io())
                    .map(new Func1<List<DeviceInfoEntity>, List<String>>() {
                        @Override
                        public List<String> call(List<DeviceInfoEntity> searchDeviceInfoEntities) {
                            List<String> devIdList = new ArrayList<String>();
                            if (searchDeviceInfoEntities != null) {
                                for (DeviceInfoEntity entity : searchDeviceInfoEntities) {
                                    devIdList.add(entity.getDeviceId());
                                }
                            }
                            return devIdList;
                        }
                    })
                    .flatMap(new Func1<List<String>, Observable<List<DeviceInfoEntity>>>() {
                        @Override
                        public Observable<List<DeviceInfoEntity>> call(final List<String> devIds) {
                            return Observable.zip(Danale.get().getPlatformDeviceInfoService().getDeviceAddedOnlineV4(1, devIds, 1, 65535),
                                    Danale.get().getPlatformDeviceInfoService().getDeviceBaseInfoV4(1, devIds),
                                    new Func2<DeviceAddedOnlineResultV4, DeviceBaseInfoResultV4, List<DeviceInfoEntity>>() {
                                        @Override
                                        public List<DeviceInfoEntity> call(DeviceAddedOnlineResultV4 deviceAddedOnlineResultV4, DeviceBaseInfoResultV4 deviceBaseInfoResultV4) {
                                            return getDeviceInfoListFromResult(devIds,deviceAddedOnlineResultV4.getDeviceAddedOnlineInfoList(),deviceBaseInfoResultV4.getDeviceBaseInfoList());
                                        }
                                    });
                        }
                    });
    }

    @Override
    public Observable<DeviceInfoEntity> obtainDeviceInfo(DeviceInfoEntity entity) {
        return Observable.just(entity)
                .subscribeOn(Schedulers.io())
                .map(new Func1<DeviceInfoEntity, List<String>>() {
                    @Override
                    public List<String> call(DeviceInfoEntity entity) {
                        List<String> devIdList = new ArrayList<String>();
                        devIdList.add(entity.getDeviceId());
                        return devIdList;
                    }
                })
                .flatMap(new Func1<List<String>, Observable<DeviceInfoEntity>>() {
                    @Override
                    public Observable<DeviceInfoEntity> call(final List<String> devIds) {
                        return Observable.zip(Danale.get().getPlatformDeviceInfoService().getDeviceAddedOnlineV4(1, devIds, 1, 65535),
                                Danale.get().getPlatformDeviceInfoService().getDeviceBaseInfoV4(1, devIds),
                                new Func2<DeviceAddedOnlineResultV4, DeviceBaseInfoResultV4, DeviceInfoEntity>() {
                                    @Override
                                    public DeviceInfoEntity call(DeviceAddedOnlineResultV4 deviceAddedOnlineResultV4, DeviceBaseInfoResultV4 deviceBaseInfoResultV4) {
                                        return getDeviceInfoListFromResult(devIds,deviceAddedOnlineResultV4.getDeviceAddedOnlineInfoList(),deviceBaseInfoResultV4.getDeviceBaseInfoList()).get(0);
                                    }
                                });
                    }
                });
    }


    private List<DeviceInfoEntity> getDeviceInfoListFromResult(List<String> devIds, List<DeviceAddedOnlineInfoV4> addInfoList, List<DeviceBaseInfo> baseInfoList){
        Map<String, DeviceInfoEntity> infoMap = new HashMap<String, DeviceInfoEntity>();
        if (devIds != null) {
            for (String id : devIds) {
                DeviceInfoEntity entity = new DeviceInfoEntity();
                entity.setDeviceId(id);
                infoMap.put(id, entity);
            }
        }

        for (DeviceAddedOnlineInfoV4 addInfo : addInfoList) {
            DeviceInfoEntity entity = infoMap.get(addInfo.getDeviceId());
            if (entity != null) {
                entity.setAddedState(addInfo.getAddedState());
                entity.setOwnerAccount(addInfo.getOwnerName());
                entity.setOnlineType(addInfo.getOnlineType());
            }
        }

        for(DeviceBaseInfo baseInfo : baseInfoList){
            DeviceInfoEntity entity = infoMap.get(baseInfo.getDeviceId());
            if(entity != null){
                entity.setModel(baseInfo.getProductMode());
                entity.setCompany(baseInfo.getFactoryName());
                entity.setProductType(baseInfo.getProductTypes().get(0));
                entity.setPhotoUrl(baseInfo.getPhotoPath());
            }
        }

        return new ArrayList<DeviceInfoEntity>(infoMap.values());
    }



}



